//
//  PYTable.m
//  pyfmdbdemo
//
//  Created by terry peng on 15/12/2.
//  Copyright © 2015年 pengyong. All rights reserved.
//

#import "MHTable.h"

#define MHTablePrefixUserDefaultKey @"MH_FMDBKEY"
#define MHTablePrefixBeforeKey @""
#define MHTablePrefixAfterKey @""
#define MHDataBaseName @"mhdatabase.sqlite"

@class MHFMDB;

@interface MHTable ()

@property(nonatomic,strong)NSArray *indexes;

@property(nonatomic,strong)NSString *prefixUserDefaultKey;

@property(nonatomic,strong)NSString *prefixBeforeKey;

@property(nonatomic,strong)NSString *prefixAfterKey;

@end

@implementation MHTable

- (instancetype)init
{
    if (self = [super init]) {
        if (![self.structure.structureDictory isEqual:nil]) {
            [self.db createTableWithDict:self.structure.structureDictory tableName:self.tableName];
        }
        if (self.indexes.count >0) {
            for (NSString *index in self.indexes) {
                [self.db createIndexWithField:index andTableName:self.tableName];
            }
        }
    }
    return self;
}

- (NSUInteger)count
{
    [self.db clean];
    [self.db setCurrentTableName:self.tableName];
    return [[self.db queryAll] count];
}

- (BOOL)isEmpty
{
    if ([self count] >0) {
        return false;
    }
    return true;
}

#pragma mark - has
- (BOOL)hasWhere:(NSString *)where
{
    [self.db clean];
    [self.db setCurrentTableName:self.tableName];
    [self.db whereWithString:where];
    return [[self.db queryCount] intValue] >0;
}

- (BOOL)hasFields:(NSDictionary *)fields
{
    [self.db clean];
    [self.db setCurrentTableName:self.tableName];
    [self.db whereWithDict:fields];
    return [[self.db queryCount] intValue] >0;
}

#pragma mark - origin sql
- (NSArray *)executeQueryWithSql:(NSString *)sql
{
    return [self.db excuteQueryWithSql:sql];
}

- (BOOL)executeUpdateWithSql:(NSString *)sql
{
    return [self.db excuteUpdateWithSql:sql];
}

#pragma mark - getter setter
- (MHFMDB *)db
{
    if (!_db) {
        _db = [MHFMDB dbWithDbName:self.databaseName];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSString *userID = [userDefaults objectForKey:self.prefixUserDefaultKey];
        _db.prefix = userID==nil ? [NSString stringWithFormat:@"%@%@",self.prefixBeforeKey,self.prefixAfterKey]:[NSString stringWithFormat:@"%@%@%@",self.prefixBeforeKey,userID,self.prefixAfterKey];
    }
    
    return _db;
}

- (NSString *)databaseName
{
    if (_databaseName) {
        return _databaseName;
    }
    _databaseName = MHDataBaseName;
    return _databaseName;
}

- (NSString *)prefixAfterKey
{
    if (_prefixAfterKey) {
        return  _prefixAfterKey;
    }
    _prefixAfterKey = MHTablePrefixAfterKey;
    return _prefixAfterKey;
}

- (NSString *)prefixBeforeKey
{
    if (_prefixBeforeKey) {
        return _prefixBeforeKey;
    }
    _prefixBeforeKey = MHTablePrefixBeforeKey;
    return _prefixBeforeKey;
}

- (NSString *)prefixUserDefaultKey
{
    if (_prefixUserDefaultKey) {
        return _prefixUserDefaultKey;
    }
    _prefixUserDefaultKey = MHTablePrefixUserDefaultKey;
    return _prefixUserDefaultKey;
}

- (MHStructure *)structure
{
    if (!_structure) {
        _structure = [[MHStructure alloc] init];
    }
    return _structure;
}

- (NSString *)lastSql
{
    return self.db.lastSql;
}

- (NSString *)databasePath
{
    return self.db.dbPath;
}


@end

